
import threading, time

def run1():
    print('grab the first part data')
    lock.acquire()
    global num
    num += 1
    lock.release()
    return num

def run2():
    print('grab the second part data')
    lock.acquire()
    global num2
    num2 += 1
    lock.release()
    return num2

def run3():
    lock.acquire()
    res = run1()
    print('-------between run1 and run2-------')
    res2 = run2()
    lock.release()
    print(res, res2)

num, num2 = 0, 0
# lock = threading.Lock()    #互斥锁如果嵌套了多个锁之后，会将自己锁死永远都出不来了
lock = threading.RLock()    #使用递归锁，它相当于一个字典，记录了锁的门与锁的对应值，当开门的时候会根据对应来开锁

for i in range(10):
    t = threading.Thread(target=run3)
    t.start()
while threading.active_count() != 1: #用来显示当前存活的线程数量
    print(threading.active_count())
else:
    print('----------all threads done----------')
    print(num, num2)